<?php
namespace pfp\lib\permsManager_checks {
    use pfp\lib as pfpLib;
    
    \defined( '_JEXEC' ) or die( 'Restricted access' );

    /**
     * Description of http_GET_Request
     *
     * @author George Zeakis<zeageorge@google.com>
     */
    class http_GET_Request {

        /**
         *
         * @var array
         */
        protected $_events = array();

        /**
         *
         * @var array
         */
        protected $_vars = \NULL;

        public function __construct() {
            $this->registerEvents();
        }

        private function registerEvents(){
            $this->_events = array(
                'event_onStart'                            =>'event_onStart',
                'event_onError'                            =>'event_onError',
                'event_onWarning'                          =>'event_onWarning',

                'event_onBefore_checkIfUserHasAccessTo_getRequest'=>'event_onBefore_checkIfUserHasAccessTo_getRequest',
                'event_onAfter_checkIfUserHasAccessTo_getRequest'=>'event_onAfter_checkIfUserHasAccessTo_getRequest',
            );
            pfpLib\eventManager::registerEvents($this->_events);
        }

        /**
         * 
         * @param array $http_GET_Requests_from_user_resources
         * @param array $getRequest
         * @return boolean Returns TRUE if user has access to $getRequest, FALSE if user has not access to $getRequest, 
         *                    or throws an codeExecution_FlowControlException(array('return'=>FALSE)) on error
         * @throws codeExecution_FlowControlException
         */
        public function check(array $http_GET_Requests_from_user_resources, array $getRequest){
            $tmp = __FUNCTION__."_tmp"; if (isset($this->_vars["$tmp"])){ unset($this->_vars["$tmp"]); }
            try {
                $this->_vars["$tmp"]["http_GET_Requests_from_user_resources"] = (array)$http_GET_Requests_from_user_resources;
                $this->_vars["$tmp"]["getRequest"] = $getRequest;

                $eventData = array('event_name'=>$this->_events['event_onBefore_checkIfUserHasAccessTo_getRequest'],'args'=>&$this->_vars); pfpLib\eventManager::riseEvent($eventData);

                $hasAccess = \FALSE;
                foreach ($this->_vars["$tmp"]["http_GET_Requests_from_user_resources"] as $value) {
                    $hasProgramAccess = $hasControllerAccess = $hasActionAccess = $hasParamsAccess = \FALSE;
                    
                    // if program value type is string (s) or regex (r)...
                    if ($value["resource"]["0"]["0"]=="s"){
                        if ($this->_vars["$tmp"]["getRequest"]["program_name"]==$value["resource"]["0"]["1"]){
                            $hasProgramAccess = \TRUE;
                        }
                    } elseif ($value["resource"]["0"]["0"]=="r") {
                        $preg_match_results = @\preg_match($value["resource"]["0"]["1"], $this->_vars["$tmp"]["getRequest"]["program_name"]);
                        if ($preg_match_results===1){
                            $hasProgramAccess = \TRUE;
                        } elseif ($preg_match_results===\FALSE){
                            $preg_match_error_results = @\preg_last_error();
                            $er_msg = "WARNING::Regex preg_match error:";
                            if ($preg_match_error_results!==0){
                                $er_msg .= pfpLib\Tools::PCRE_preg_last_error2string($preg_match_error_results);
                            } else {
                                $er_msg .="invalid regex at ".__CLASS__." program, of resource id ".$value["id"];
                            }
                            $eventData = array('event_name'=>$this->_events['event_onWarning'],
                                           'args'=>&$this->_vars, "WARNING"=>array("warning_msg"=>$er_msg)); pfpLib\eventManager::riseEvent($eventData);
                        }     
                    }

                    // if controller value type is string (s) or regex (r)...
                    if ($value["resource"]["1"]["0"]=="s"){
                        if ($this->_vars["$tmp"]["getRequest"]["controller_name"]==$value["resource"]["1"]["1"]){
                            $hasControllerAccess = \TRUE;
                        }
                    } elseif ($value["resource"]["1"]["0"]=="r") {
                        $preg_match_results = @\preg_match($value["resource"]["1"]["1"], $this->_vars["$tmp"]["getRequest"]["controller_name"]);
                        if ($preg_match_results===1){
                            $hasControllerAccess = \TRUE;
                        } elseif ($preg_match_results===\FALSE){
                            $preg_match_error_results = @\preg_last_error();
                            $er_msg = "WARNING::Regex preg_match error:";
                            if ($preg_match_error_results!==0){
                                $er_msg .= pfpLib\Tools::PCRE_preg_last_error2string($preg_match_error_results);
                            } else {
                                $er_msg .="invalid regex at ".__CLASS__." controller, of resource id ".$value["id"];
                            }
                            $eventData = array('event_name'=>$this->_events['event_onWarning'],
                                           'args'=>&$this->_vars, "WARNING"=>array("warning_msg"=>$er_msg)); pfpLib\eventManager::riseEvent($eventData);
                        }     
                    }

                    // if action value type is string (s) or regex (r)...
                    if ($value["resource"]["2"]["0"]=="s"){
                        if ($this->_vars["$tmp"]["getRequest"]["action_name"]==$value["resource"]["2"]["1"]){
                            $hasActionAccess = \TRUE;
                        }
                    } elseif ($value["resource"]["2"]["0"]=="r") {
                        $preg_match_results = @\preg_match($value["resource"]["2"]["1"], $this->_vars["$tmp"]["getRequest"]["action_name"]);
                        if ($preg_match_results===1){
                            $hasActionAccess = \TRUE;
                        } elseif ($preg_match_results===\FALSE){
                            $preg_match_error_results = @\preg_last_error();
                            $er_msg = "WARNING::Regex preg_match error:";
                            if ($preg_match_error_results!==0){
                                $er_msg .= pfpLib\Tools::PCRE_preg_last_error2string($preg_match_error_results);
                            } else {
                                $er_msg .="invalid regex at ".__CLASS__." action, of resource id ".$value["id"];
                            }
                            $eventData = array('event_name'=>$this->_events['event_onWarning'],
                                           'args'=>&$this->_vars, "WARNING"=>array("warning_msg"=>$er_msg)); pfpLib\eventManager::riseEvent($eventData);
                        }     
                    }

                    // if action_params value type is string (s) or regex (r)...
                    if ($value["resource"]["3"]["0"]=="s"){
                        if ($this->_vars["$tmp"]["getRequest"]["action_params_asString"]==$value["resource"]["3"]["1"]){
                            $hasParamsAccess = \TRUE;
                        }
                    } elseif ($value["resource"]["3"]["0"]=="r") {
                        $preg_match_results = @\preg_match($value["resource"]["3"]["1"], $this->_vars["$tmp"]["getRequest"]["action_params_asString"]);
                        if ($preg_match_results===1){
                            $hasParamsAccess = \TRUE;
                        } elseif ($preg_match_results===\FALSE){
                            $preg_match_error_results = @\preg_last_error();
                            $er_msg = "WARNING::Regex preg_match error:";
                            if ($preg_match_error_results!==0){
                                $er_msg .= pfpLib\Tools::PCRE_preg_last_error2string($preg_match_error_results);
                            } else {
                                $er_msg .="invalid regex at ".__CLASS__." params, of resource id ".$value["id"];
                            }
                            $eventData = array('event_name'=>$this->_events['event_onWarning'],
                                           'args'=>&$this->_vars, "WARNING"=>array("warning_msg"=>$er_msg)); pfpLib\eventManager::riseEvent($eventData);
                        }
                    }

                    if ($hasProgramAccess===\TRUE && $hasControllerAccess===\TRUE && $hasActionAccess===\TRUE && $hasParamsAccess===\TRUE){
                        $hasAccess = \TRUE; break;
                    }
                }

                $this->_vars["$tmp"]["hasAccess_results"] = $hasAccess; unset($hasAccess); 
                $eventData = array('event_name'=>$this->_events['event_onAfter_checkIfUserHasAccessTo_getRequest'],'args'=>&$this->_vars); pfpLib\eventManager::riseEvent($eventData);
                
                $re = $this->_vars["$tmp"]["hasAccess_results"]; unset($this->_vars["$tmp"]);
                return (boolean)$re;
            } catch (pfpLib\codeExecution_FlowControlException $exc) {
                $this->_vars["$tmp"]["errors"] = $exc;
                $eventData = array('event_name'=>$this->_events['event_onError'],'args'=>&$this->_vars); pfpLib\eventManager::riseEvent($eventData);
                unset($this->_vars["$tmp"]); 
                throw new pfpLib\codeExecution_FlowControlException(array('return'=>\FALSE), $exc->getMessage()); return \FALSE;
            }
        }

        public function __destruct() {}


    }
}
?>
